package com.easypan.controller;

import com.easypan.Service.FileShareService;
import com.easypan.annotation.GlobalInterceptor;
import com.easypan.entity.constants.Constants;
import com.easypan.entity.dto.SessionShareDto;
import com.easypan.entity.dto.SessionwebUserDto;
import com.easypan.entity.vo.R;
import com.easypan.enums.ResponseCodeEnum;
import com.easypan.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;

@RestController
@RequestMapping("/showShare")
public class waibufxController extends CommonFileController {

    @Autowired
    FileShareService fileShareService;

    //通过分享的ID来查询分享的用户信息
    @PostMapping("/getShareLoginInfo")
    public R getShareLoginInfo(String shareId, HttpSession session) {
        R r = fileShareService.getShareLoginInfo(session, shareId);

        return r;
    }

    //获取分享信息
    @PostMapping("/getShareInfo")
    public R getShareInfo(String shareId, HttpSession session) {
        R r = fileShareService.getShareInfo(shareId, session);
        return r;
    }

    //    接口状态: 开发中
    //    接口URL: POSThttp://localhost:7090/api/showShare/checkShareCode
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
    //效验分享码
    @PostMapping("/checkShareCode")
    public R checkShareCode(String shareId, HttpSession session, String code) {
        R r = fileShareService.checkShareCode(shareId, session, code);
        return r;
    }

    //    接口状态: 开发中
    //    接口URL: POSThttp://localhost:7090/api/showShare/loadFileList
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
    //获取文件列表

    @PostMapping("/loadFileList")
    public R loadFileList(String shareId, HttpSession session, String filePid) {
        R r = fileShareService.loadFileList(shareId, session, filePid);
        //进行分页查询
        return r;
    }

    ///    接口状态: 开发中
    //    接口URL: POSThttp://localhost:7090/api/showShare/getFolderInfo
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
    //获取目录信息
    @PostMapping("/getFolderInfo")
    public R getFolderInfo(String shareId, HttpSession session, String path) {
        R r = fileShareService.getFolderInfo(shareId, session, path);
        //进行分页查询
        return r;
    }

    ///http://localhost:7090/api/showShare/getFile
///
    // http://easypan.wuhancoder.com/api/showShare/getFile/b441LuEpd4j3YAs/7879278633
    //获取文件信息 --比如视频文件 音频文件在线看
    @RequestMapping("/getFile/{shareId}/{fileId}")

    public R getfile(@PathVariable("shareId") String shareId, @PathVariable("fileId") String fileId, HttpServletResponse httpServletResponse, HttpSession session) {
        SessionShareDto sessionShareDto = checkShare(session, shareId);
        R file = super.getFile(httpServletResponse, fileId, sessionShareDto.getShareUserId());
        return file;

    }

    private SessionShareDto checkShare(HttpSession session, String shareId) {
        SessionShareDto shareDto = (SessionShareDto) session.getAttribute(Constants.SESSION_SHARE_KEY + shareId);
        if (null == shareDto) {
            throw new BusinessException(ResponseCodeEnum.CODE_903);
        }
        if (shareDto.getExpireTime() != null && new Date().after(shareDto.getExpireTime())) {
            throw new BusinessException(ResponseCodeEnum.CODE_902);
        }
        return shareDto;
    }

    ///GET
    //	http://easypan.wuhancoder.com/api/showShare/ts/getVideoInfo/b441LuEpd4j3YAs/5819018994
    //视频播放
    @GetMapping("/ts/getVideoInfo/{shareId}/{fileId}")

    public R spbf(@PathVariable("shareId") String shareId, @PathVariable("fileId") String fileId, HttpServletResponse httpServletResponse, HttpSession session) {
        SessionShareDto sessionShareDto = checkShare(session, shareId);
        R file = super.getFile(httpServletResponse, fileId, sessionShareDto.getShareUserId());
        return file;
    }

    ///scheme
    //	http
    //host
    //	easypan.wuhancoder.com
    //filename
    //	/api/showShare/createDownloadUrl/b441LuEpd4j3YAs/2501942252
    //文件下载
    @PostMapping ("/createDownloadUrl/{shareId}/{fileId}")

    public  R createDownloadUrl(@PathVariable("shareId") String shareId, @PathVariable("fileId") String fileId, HttpServletResponse httpServletResponse, HttpSession session)
    {
        SessionShareDto shareSessionDto = checkShare(session, shareId);
        R r= fileInfoService.createDownloadUrl(fileId,shareSessionDto.getShareUserId());
        //思路 - 下载链接 --
        return r;
    }
    ///GET
    //
    //scheme
    //	http
    //host
    //	easypan.wuhancoder.com
    //filename
    //	/api/showShare/download/WYGhTsjfWbM6tnqVr46dZbfs8gF4fkIQGZkNtNayfnR9H0NZXw
    @GetMapping("/download/{code}")

    public R download(@PathVariable("code")String code, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
        R r=fileInfoService.download(request,response,code);
        //HttpServletRequest request, HttpServletResponse response, String code
        return r;
    }


   /// http://easypan.wuhancoder.com/api/showShare/saveShare
 //保存分享
    //shareId: b441LuEpd4j3YAs--分享
   //shareFileIds: 2842725735--保存的文件
   //myFolderId: B1YQGJ8270//保存所在目录
    @PostMapping("/saveShare")

    R saveShare(HttpSession session,String shareId,String shareFileIds,String  myFolderId)
    {
        //移动文件思路


        return R.error("抱歉 自己下载了上传 懒得写");
    }

}
